package com.example.mapper;

import com.example.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper // 关键注解：Spring在启动时会自动扫描该接口，并为其生成动态代理实现类，然后注册到IoC容器中。
public interface UserInfoMapper {

    /**
     * 查询所有用户信息
     * @return 用户信息列表
     */
    @Select("select * from user_info")
    // @Select注解：定义了执行该方法的SQL语句。
    // 由于配置了map-underscore-to-camel-case=true，结果会自动映射到UserInfo对象的属性。
    List<UserInfo> selectAll();

    @Select("select id, username, `password`, age, gender, phone, " +
            "delete_flag as deleteFlag, " +
            "create_time as createTime, " +
            "update_time as updateTime " +
            "from user_info")
    List<UserInfo> queryAllUser();

    @Select("select id, username, `password`, age, gender, phone, delete_flag, create_time, update_time from user_info")
    @Results(id = "resultMap", value = {
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    List<UserInfo> AllUser();

    @Select("select id, username, `password`, age, gender, phone, delete_flag, create_time, update_time " +
            "from user_info where id = #{userid}")
    @ResultMap("resultMap")
    UserInfo queryById(@Param("userid") Integer id);



    @Select("select id, username, `password`, age, gender, phone, " +
            "delete_flag, create_time, update_time from user_info")
    List<UserInfo> queryAllUser2();


//    @Select("select * from user_info where id = #{id}")
//    UserInfo selectById(Integer id);


    @Select("SELECT * FROM user_info WHERE id = #{userId}")
    UserInfo selectById(@Param("userId") Integer id); // SQL中的占位符名称必须是userId
// 此时，#{}内的名称必须和@Param("userId")中定义的名称一致。


//    @Insert("INSERT INTO user_info (username, password, age, gender, phone) " +
//            "VALUES (#{username}, #{password}, #{age}, #{gender}, #{phone})")
//    Integer insert(UserInfo userInfo); // 返回值为受影响的行数

    @Options(useGeneratedKeys = true, keyProperty = "id") // 关键注解
    @Insert("INSERT INTO user_info (username, password, age, gender, phone) " +
            "VALUES (#{username}, #{password}, #{age}, #{gender}, #{phone})")
    Integer insert(UserInfo userInfo);

    @Select("select * from user_info where username = #{name}")
    List<UserInfo> selectByName(String name);

    @Select("select * from user_info where id = ${id}")
    UserInfo selectById2(Integer id);

    @Delete("DELETE FROM user_info WHERE id = #{id}")
    Integer deleteById(Integer id); // 返回受影响的行数

    @Update("UPDATE user_info SET password = #{password} WHERE id = #{id}")
    Integer updateById(UserInfo userInfo); // 使用对象传递参数，更新部分字段








    @Select("select * from user_info where username = '${name}'")
    List<UserInfo> selectByName2(String name);
    @Select("SELECT * FROM `user_info` where age = #{age} and delete_flag = #{flag}")
    List<UserInfo> selectByAge(@Param("flag") Integer deleteFlag, Integer age);

    @Insert("insert into user_info (username,password,age) values (#{username},#{password},#{age})")
    Integer insertUser(UserInfo userInfo);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into user_info (username,password,age) values (#{userInfo.username},#{userInfo.password},#{userInfo.age})")
    Integer insertUser2(@Param("userInfo") UserInfo userInfo);

//    @Delete("delete from user_info where id = #{id}")
//    Integer deleteById(Integer id);

//    @Update("update user_info set password = #{password} where id = #{id}")
//    Integer updateById(UserInfo userInfo);

}